﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.Background;
using Model.Cms;
using Model.Config;
using BLL.Cms;
using Com.Ompchina.Lib.Draw;
namespace Com.Ompchina.Web.admin.cms
{
    public partial class add_block : AddFilePage, IAddFile
    {
        protected DateTime ExecuteStart = new DateTime(), ExecuteStop = new DateTime();
        protected void Page_Load(object sender, EventArgs e)
        {
            ExecuteStart = new DateTime();
            base.SetCKFinder("block", ckeBlock);
            if (!IsPostBack) 
            {
                base.InitOnlineStatus(txtOnline, ddlHour, ddlMinute);
                this.ClearFile();
                if (PageAction.Equals("update")) { this.InitSingleInfo(base.GetUpdateID()); }
            }
        }

        #region IAddFile 成员

        public bool CheckFile()
        {
            if (!string.IsNullOrEmpty(fuUploader.GetUploadFilename()))
                return true;
            else if (fuUploader.DefaultFileCtrl.HasFile)
            {
                AllowFormat[] allow = new AllowFormat[] { AllowFormat.Image, AllowFormat.Swf };
                if (!fuUploader.CheckLength(fuUploader.DefaultFileCtrl, 2 * 1024))
                {
                    base.SetMessage(false, "您上传的文件必须小于2M");
                    return false;
                }
                else if (!fuUploader.CheckFormat(fuUploader.DefaultFileCtrl, allow))
                {
                    base.SetMessage(false, "您上传的文件必须是swf或者图片格式");
                    return false;
                }
                return true;
            }
            return true;
        }

        public void ClearFile()
        {
            fuUploader.ClearUploadFilename();
        }

        #endregion

        #region IAddInfo 成员

        public bool CheckInput()
        {
            if (ddlTerm.SelectedIndex == 0)
            {
                base.SetMessage(false, "请选择广告点位");
                return false;
            }
            else if (ddlType.SelectedIndex == 0)
            {
                base.SetMessage(false, "请选择广告类别");
                return false;
            }
            else if (ddlType.SelectedValue.Equals("f") && string.IsNullOrEmpty(txtSize.Text.Trim()))
            {
                base.SetMessage(false, "flash点位务必输入尺寸");
                return false;
            }
            else if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                base.SetMessage(false, "请输入广告名称");
                return false;
            }
            return true;
        }

        public void InitSingleInfo(int id)
        {
            if (id > 0)
            {
                BlockInfo info = new BlockBLL().GetBlockByID(id);
                if (info.BlockID > 0)
                {
                    ddlTerm.DefaultSelect = info.BlockCategory.TermID.ToString();
                    ddlType.DefaultSelect = info.BlockType;
                    txtName.Text = info.BlockName;
                    fuUploader.SetUploadFilename(info.Filepath);
                    txtSize.Text = info.BlockSize;
                    txtRank.Text = info.Rank.ToString();
                    txtLink.Text = string.IsNullOrEmpty(info.Link) ? "" : Server.HtmlDecode(info.Link);
                    ckeBlock.Text = string.IsNullOrEmpty(info.Content) ? "" : Server.HtmlDecode(info.Content);
                    cbOnline.Checked = info.IsOnline.Equals("y");
                    txtOnline.Text = info.OnlineTime.ToString("yyyy-MM-dd");
                    ddlHour.DefaultSelect = info.OnlineTime.ToString("HH");
                    ddlMinute.DefaultSelect = info.OnlineTime.ToString("mm");
                    if (PageAction.Equals("update"))
                        hfID.Value = id.ToString();
                }
            }
        }

        #endregion

        #region 页面私有方法
        /// <summary>
        /// 创建区块对象
        /// </summary>
        /// <returns></returns>
        private BlockInfo CreateBlock()
        {
            BlockInfo info = new BlockInfo();
            info.BlockCategory = new TermInfo();
            info.BlockCategory.TermID = int.Parse(ddlTerm.SelectedValue);
            info.BlockType = ddlType.SelectedValue;
            info.BlockName = txtName.Text.Trim();
            info.BlockSize = txtSize.Text.Trim();
            info.Filepath = fuUploader.GetUploadFilename();
            int rank = 1;
            int.TryParse(txtRank.Text.Trim(), out rank);
            info.Rank = rank;
            info.Link = string.IsNullOrEmpty(txtLink.Text.Trim()) ? "" : Server.HtmlEncode(txtLink.Text.Trim());
            info.LinkTarget = "";
            info.Content = string.IsNullOrEmpty(ckeBlock.Text.Trim()) ? "" : Server.HtmlEncode(ckeBlock.Text.Trim());
            info.IsOnline = cbOnline.Checked ? "y" : "n";
            info.OnlineTime = DateTime.Parse(txtOnline.Text.Trim() + string.Format(" {0}:{1}:00", ddlHour.SelectedValue, ddlMinute.SelectedValue));
            info.Staff = base.LoginStaff;
            if (!hfID.Value.Equals("0"))
                info.BlockID = int.Parse(hfID.Value);
            return info;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput() && this.CheckFile())
            {
                if (fuUploader.DefaultFileCtrl.HasFile)
                {
                    fuUploader.SaveFile(fuUploader.DefaultFileCtrl
                        , AbsoluteRoot, "uploads/block/b_");
                }
                BlockInfo info = this.CreateBlock();
                BlockBLL bll = new BlockBLL();
                if (hfID.Value.Equals("0"))
                {
                    if (bll.InsertBlock(info) > 0)
                    {                        
                        this.SetMessage(true, "新增广告成功");
                        base.Redirect("list_block.aspx?action=list" + base.GetQueryStringAdd(), 1);
                    }
                    else
                        this.SetMessage(false, "新增广告错误，请稍后重试");
                }
                else
                {                    
                    if (bll.UpdateBlock(info))
                    {                        
                        this.SetMessage(true, "修改广告成功");
                        base.Redirect("list_block.aspx?action=list" + base.GetQueryStringAdd(), 1);
                    }
                    else
                        this.SetMessage(false, "修改广告错误，请稍后重试");
                }
            }
        }
        #endregion
        
    }
}
